<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<div class="setp_top">
	<div class="setp_top_btn">
		&nbsp;<s:if test="#request.isCreate == 1">
			<input type="button" class="btn_ok" onclick="nextStep()" />
		</s:if>
		<input type="button" class="btn_cancel" onclick="prevStep()" />
	</div>
	<s:property value="#typeTxt"/>&nbsp;<s:property value="#workStatusTxt"/>
</div>
<div class="setp_main">
	<div class="fee_border" style="margin-top: 5px;">
		<input type="hidden" id="teamSum" name="teamSum"/>
		<s:set var="teamSum" value="0"></s:set>
		<div class="fee_add_div"><a href="javascript: addTeams()" class="fee_add">添加团队成员</a></div>
		<span class="fee_place">&nbsp;作业团队</span>
		<table id="teamTable" class="tab1" cellpadding="0" cellspacing="0" style="width: 100%; margin-top: 10px;">
			<thead>
				<tr>
					<td width="120px">团队成员名称</td>
					<td width="80px">权限</td>
					<td class="stress_bg">权限说明</td>
					<td width="50px">&nbsp;</td>
				</tr>
			</thead>
			<s:set var="teamSum" value="#teamSum + 1"></s:set>
			<tr id="teamTr_<s:property value="#teamSum"/>">
				<td>
					<span><s:property value="work.employee.nickname"/></span>
					<span style="display: none">
						<input type="text" value="<s:property value="work.employee.nickname"/>" id="employee_<s:property value="#teamSum"/>" name="employee_<s:property value="#teamSum"/>" 
							onkeyup="seachDataByKeyword('searchEmployeeBykeyword.action?type=0',this.value,'employee_<s:property value="#teamSum"/>','employeeId_<s:property value="#teamSum"/>',0,200);" class="input_normal"/>
						<input type="hidden" value="<s:property value="work.employee.id"/>" id="employeeId_<s:property value="#teamSum"/>" name="employeeId_<s:property value="#teamSum"/>"/>
						<input type="hidden" value="author" id="auth_<s:property value="#teamSum"/>" name="auth_<s:property value="#teamSum"/>"/>
					</span>
				</td>
				<td>
					创建
				</td>
				<td class="nowrap">
					订单的创建、修改、删除及归档
				</td>
				<td>
					<span>
						<a id="a_fee_edit_<s:property value="#teamSum"/>" class="fee_edit" href="javascript: toEditTeamsStyle('<s:property value="#teamSum"/>')"></a>
					</span>
					<span style="display: none">
						<a id="a_fee_ok_<s:property value="#teamSum"/>" class="fee_ok" href="javascript: saveTeams('<s:property value="#teamSum"/>')"></a>
					</span>
				</td>
			</tr>
			<s:iterator value="%{#request.workTeams}" status="sta">
				<s:set var="teamSum" value="#teamSum + 1"></s:set>
				<tr class="<s:if test="#sta.isOdd()">tr1</s:if>" id="teamTr_<s:property value="#teamSum"/>">
					<td>
						<span>
							<s:property value="employee.nickname"/>
						</span>
						<span style="display: none">
							<input type="text" value="<s:property value="employee.nickname"/>" id="employee_<s:property value="#teamSum"/>" name="employee_<s:property value="#teamSum"/>" 
								onkeyup="seachDataByKeyword('searchEmployeeBykeyword.action?type=0',this.value,'employee_<s:property value="#teamSum"/>','employeeId_<s:property value="#teamSum"/>',0,200);" class="input_normal"/>
							<input type="hidden" value="<s:property value="employee.id"/>" id="employeeId_<s:property value="#teamSum"/>" name="employeeId_<s:property value="#teamSum"/>"/>
							<input type="hidden" value="<s:property value="id"/>" id="workTeamId_<s:property value="#teamSum"/>" name="workTeamId_<s:property value="#teamSum"/>"/>
						</span>
					</td>
					<td>
						<span>
							<s:if test="auth == 1">
								编辑
							</s:if>
							<s:else>
								浏览
							</s:else>
						</span>
						<span style="display: none">
							<select class="select_normal" id="auth_<s:property value="#teamSum"/>" name="auth_<s:property value="#teamSum"/>">
								<option value="1" <s:if test="auth == 1">selected</s:if>>编辑</option>
								<option value="2" <s:if test="auth == 2">selected</s:if>>浏览</option>
							</select>
						</span>
					</td>
					<td>
						<s:if test="auth == 1">
							订单的修改
						</s:if>
						<s:else>
							浏览订单
						</s:else>
					</td>
					<td>
						<span>
							<a id="a_fee_edit_<s:property value="#teamSum"/>" class="fee_edit" href="javascript: toEditTeamsStyle('<s:property value="#teamSum"/>')"></a>
						</span>
						<span style="display: none">
							<a id="a_fee_ok_<s:property value="#teamSum"/>" class="fee_ok" href="javascript: saveTeams('<s:property value="#teamSum"/>')"></a>
						</span>
						<a class="fee_del" href="javascript: deleteTeams('<s:property value="#teamSum"/>')"></a>
					</td>
				</tr>
			</s:iterator>
			<s:set var="teamSum" value="#teamSum + 1"></s:set>
			<tr id="teamTr_<s:property value="#teamSum"/>" style="display: none;">
				<td>
					<span style="display: none"></span>
					<span>
						<input type="text" id="employee_<s:property value="#teamSum"/>" name="employee_<s:property value="#teamSum"/>" class="input_normal"/>
						<input type="hidden" id="employeeId_<s:property value="#teamSum"/>" name="employeeId_<s:property value="#teamSum"/>"/>
						<input type="hidden" id="workTeamId_<s:property value="#teamSum"/>" name="workTeamId_<s:property value="#teamSum"/>"/>
					</span>
				</td>
				<td>
					<span style="display: none"></span>
					<span>
						<select class="select_normal" id="auth_<s:property value="#teamSum"/>" name="auth_<s:property value="#teamSum"/>">
							<option value="0">请选择</option>
							<option value="1">编辑</option>
							<option value="2">浏览</option>
						</select>
					</span>
				</td>
				<td class="nowrap">&nbsp;</td>
				<td>
					<span style="display: none">
						<a id="a_fee_edit_<s:property value="#teamSum"/>" class="fee_edit"></a>
					</span>
					<span>
						<a id="a_fee_ok_<s:property value="#teamSum"/>" class="fee_ok"></a>
					</span>
					<a id="a_fee_del_<s:property value="#teamSum"/>" class="fee_del"></a>
				</td>
			</tr>
		</table>
	</div>
</div>
<script type="text/javascript">
	
	var feeOpFlag = 0; //操作控制
	var teamSum = '<s:property value="#teamSum" />';
	var workId = '<s:property value="work.id"/>';
	
	
	//新加作业团队
	function addTeams(){
		if(feeOpFlag == 1){
			alert('请先完成正在操作中的作业团队！');
			return;
		}
		teamSum++;
		var table = $("#teamTable");
		var templateTr = table.find("tr:hidden");
		var copyTr = templateTr.clone();
		copyTr.show();
		$(copyTr).attr('id', 'teamTr_' + teamSum);
		$(copyTr).find("input,select,a").each(function(){
			var id = this.id;
			if(id && id != ''){
				var idpart1 = id.substring(0, id.lastIndexOf('_') + 1);
				this.id = idpart1 + teamSum;
				this.name = idpart1 + teamSum;
			}
		});
		var theas = $(copyTr).find("td:last a");
		
		theas.eq(0).attr('href', 'javascript: toEditTeamsStyle('+teamSum+')');
		theas.eq(1).attr('href', 'javascript: saveTeams('+teamSum+')');
		theas.eq(2).attr('href', 'javascript: deleteTeams('+teamSum+')');
		$(copyTr).find("input[id*=employee_]").each(function(){
			$(this).keyup(function(e){
				seachDataByKeyword('searchEmployeeBykeyword.action?type=0',this.value,'employee_' + teamSum,'employeeId_' + teamSum,0,200);
			})
		});
		$(copyTr).find("select[id*=auth_]").each(function(){
			$(this).change(function(e){
				var auth = $(this).val();
				var text = ' ';
				if(auth == 1){
					text = '订单的修改';
				}else if(auth == 2){
					text = '浏览订单';
				}
				$(this).parent().parent().next().text(text).css('padding', '2px');
			})
		});
		copyTr.find('td').not(':last').css('padding', '0px')
		table.find("tr:last").after(copyTr);
		feeOpFlag = 1;
	}
	
	
	var preErrorId = [];
	function saveTeams(index){
		var ispass = true;
		var errorId = [];
		
		var isAuthorChange = false;
		var auth = $('#auth_' + index).val();
		var employeeId = $('#employeeId_' + index).val();
		
		if(auth == 'author' && employeeId != ''){
			var srcAuthor = '<s:property value="work.employee.id"/>';
			if(srcAuthor != employeeId){
				if(confirm('注意：您更改了本订单创建权限，保存后，本订单的创建者将更改为:' + $('#employee_' + index).val() + '，您将失去本订单的拥有者权限，该操作不能取消，您确定吗？')){
					isAuthorChange = true;
				}else{
					return;
				}
			}
		}else if(auth == 0){
			errorId[errorId.length] = "auth_";
		}
		
		if(employeeId == ''){
			errorId[errorId.length] = "employee_";
		}
		
		for(var i=0; i<preErrorId.length; i++){
			normalCss(preErrorId[i] + index);
		}
		for(var i=0; i<errorId.length; i++){
			ispass = false;
			errorCss(errorId[i] + index);
		}
		preErrorId = errorId;
		if(!ispass){
			alert('缺少必填项或输入的值不正确！');
			return;
		}
		var besaveObj = $('#teamTr_' + index);
		besaveObj.wrap('<form id="feeFormTemp_'+index+'"></form>');
		var params = besaveObj.parent().serialize();
		besaveObj.unwrap();
		$.post("cts/workStep6.action?save=true&op=save&workId=" + workId + '&index=' + index, params, function(data){
			if(data != ''){
				if(isAuthorChange){
					cancelWork();
				}else{
					$('#workTeamId_' + index).val(data);
					toViewTeamsStyle(index);
					feeOpFlag = 0;
				}
			}else{
				alert('操作失败，请稍候再试！');
			}
	    });
	}
	
	//检查逻辑错误
	function fillLocaleText(id, type){
		if(id.indexOf('employee') != -1){
			var index = id.substring(id.indexOf('_') + 1, id.length);
			var currentTr = $('#teamTr_' + index);
			var currentEmployeeId = currentTr.find('input[id*=employeeId_]').val();
			$("#teamTable tr:visible:gt(0)").each(function(){
				var trid = this.id;
				var trindex = trid.substring(trid.indexOf('_') + 1, trid.length);
				if(index != trindex){
					var employeeid = $(this).find('input[id*=employeeId_]').val();
					if(currentEmployeeId == employeeid){
						alert('不能添加重复的团队成员');
						currentTr.find('input[id*=employeeId_]').val('');
						currentTr.find('input[id*=employee_]').val('');
						return;
					}
				}
			})
		}
	}
	
	//删除费用
	function deleteTeams(index){
		if(feeOpFlag == 1 && index != teamSum){
			alert('请先完成正在操作中的作业团队！');
			return;
		}
		var workTeamId = $('#workTeamId_' + index).val();
		var text = '确定要删除该作业团队吗？';
		if(workTeamId != ''){
			if(confirm(text)){
				$.post("cts/workStep6.action?save=true&op=delete&workTeamId=" + workTeamId, null, function(data){
					if(data == 1){
						$('#teamTr_' + index).remove();
					}else{
						alert('操作失败，请稍候再试！');
					}
			    });
			}
		}else{
			$('#teamTr_' + index).remove();
			feeOpFlag = 0;
		}
	}
	
	function toEditTeamsStyle(index){
		if(feeOpFlag == 1){
			alert('请先完成正在操作中的作业团队！');
			return;
		}
		var obj = $('#teamTr_' + index);
		obj.find('td').each(function(){
			$(this).find('span:eq(0)').hide();
			$(this).find('span:eq(1)').show();
		})
		obj.find("select[id*=auth_]").each(function(){
			$(this).change(function(e){
				var auth = $(this).val();
				var text = ' ';
				if(auth == 1){
					text = '订单的修改';
				}else if(auth == 2){
					text = '浏览订单';
				}
				$(this).parent().parent().next().text(text).css('padding', '2px');
			})
		});
	}
	
	function toViewTeamsStyle(index){
		var obj = $('#teamTr_' + index).find('td');
		obj.each(function(){
			$(this).find('span:eq(0)').show();
			$(this).find('span:eq(1)').hide();
		})
		obj.not(':last').css('padding', '2px');
		obj.eq(0).find('span:eq(0)').text(obj.eq(0).find('span:eq(1)').find('input[id*=employee_]').val());
		obj.eq(1).find('span:eq(0)').text(obj.eq(1).find('span:eq(1)').find('select option:selected').text());
	}
	
</script>